namespace Ultimate.Login.Controls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using Ultimate.DAL.CollectionClasses;
	using Ultimate.DAL;
	using Ultimate.DAL.HelperClasses;
	using Ultimate.DAL.EntityClasses;
	using Ultimate.DAL.FactoryClasses;
	using SD.LLBLGen.Pro.ORMSupportClasses;
	using System.Web.Security;

	/// <summary>
	///		Control that handles user login.
	/// </summary>
	public class Login : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox userName;
		protected System.Web.UI.WebControls.TextBox Password;
		protected System.Web.UI.WebControls.Literal ErrorMessage;
		protected System.Web.UI.WebControls.CheckBox PersistLogin;
		protected System.Web.UI.WebControls.TextBox textbox1;
		protected System.Web.UI.WebControls.Button Button1;

		/// <summary>
		/// Nothing done here.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		/// <summary>
		/// Validates the users credentials and redirects them back to the page they attempted to access.
		/// </summary>
		/// <param name="o"></param>
		/// <param name="e"></param>
		protected void ValidateCredentials(System.Object o,System.EventArgs e)
		{
			PlayerCollection pc = new PlayerCollection();
			PredicateExpression A = new PredicateExpression();
			A.Add(PredicateFactory.CompareValue(PlayerFieldIndex.Email, ComparisonOperator.Equal, userName.Text));
			A.AddWithAnd(PredicateFactory.CompareValue(PlayerFieldIndex.Password, ComparisonOperator.Equal, FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Text+userName.Text,"md5")));
			pc.GetMulti(A);

			if (pc.Count > 0)
			{
				PlayerEntity pe = (PlayerEntity)pc.Items[0];
				
				FormsAuthentication.RedirectFromLoginPage(pe.UserID.ToString(),PersistLogin.Checked);
			}
			else
			{
				ErrorMessage.Text="Invalid Login";
			}

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
